package com.hhd.flow.engine.variable.impl;

import com.alibaba.fastjson.JSONObject;
import com.hhd.flow.engine.utils.CommonUtils;
import com.hhd.flow.engine.variable.FlowVariableDefinition;
import com.hhd.flow.engine.variable.FlowVariablesContainer;
import com.hhd.flow.engine.vo.FlowEnum;
import com.tang.script.variable.Variable;
import com.tang.script.variable.VariablesType;

import java.lang.reflect.Field;
import java.util.List;

public class JSONFlowVariablesContainer implements FlowVariablesContainer {
    private final Class entityClass;
    private final Object entityObject;

    public JSONFlowVariablesContainer(Class entityClass, Object entityObject) {
        this.entityClass = entityClass;
        this.entityObject = entityObject;
    }

    @Override
    public List<FlowVariableDefinition> getFlowVariableDefinitions() {
        List<FlowVariableDefinition> res = CommonUtils.newList();
        Field[] allFields = CommonUtils.getAllFields(entityClass);
        for (int i = 0; i < allFields.length; i++) {
            FlowVariableDefinition def = new FlowVariableDefinition();
            Field field = allFields[i];
            def.setVariableTitle(field.getName());
            def.setVariableName(field.getName());
            def.setGroupName("单据变量");
            def.setComponent(VariableComponent.TestSelect);
            def.setVariableType(VariablesType.string);
            if (field.getType() == Integer.class) {
                def.setVariableType(VariablesType.number);
            }
            res.add(def);
        }
        return res;
    }

    enum VariableComponent implements FlowEnum {
        InputComponent("输入框"),
        TestSelect("输入框");

        private final String title;

        VariableComponent(String title) {
            this.title = title;
        }

        @Override
        public String getTitle() {
            return null;
        }
    }

    @Override
    public Object getValue(Variable variable) {
        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(this.entityObject));
        return json.get(variable.getValue());
    }
}
